

let tbody = document.querySelector("tbody")

getScoreList()
/* 获取学生成绩列表函数 */
function getScoreList() {
  let loadingScoreListData = axios.get('/score/list')
  loadingScoreListData
    .then(res => {
      let { data: result } = res
      if (result.code === 0) {
        let scoreListData = result.data
        let html = ''
        for (let key in scoreListData) {
          let scoreTd = ''
          scoreTd = scoreListData[key].score.reduce((sum, value, index) =>
            sum + `<td class="score">${value}<input type="text" data-id='${key}' data-batch='${index + 1}'></td>`
            , '')
          html += `
          <tr>
          <th scope="row">${key}</th>
          <td>${scoreListData[key].name}</td>
          ${scoreTd}
        </tr> 
          `
        }
        tbody.innerHTML = html
        //为tbody里所有的inputs监听失去焦点事件
        let inputDoms = tbody.querySelectorAll("input")
        inputDoms.forEach(input => {
          input.addEventListener("blur", function () {
            let data = {
              "stu_id": input.dataset.id,
              "batch": input.dataset.batch,
              "score": input.value
            }
            axios.post('/score/entry', data)
              .then(res => {
                const { data: result } = res
                if (result.code === 0) {
                  console.log(result);
                  toastr.success(result.message);
                  getScoreList()
                }
              })
              .catch(res => {

              })
          })
        })
      }
    })
    .catch(res => {
    })



}


//事件委托为成绩input输入框绑定双击事件
tbody.addEventListener("dblclick", function (e) {
  let dom = e.target
  if (dom.nodeName === 'TD' && dom.classList.contains("score")) {
    let inputDom = dom.querySelector("input")
    inputDom.style.display = "block"
    inputDom.value = dom.innerText
    inputDom.focus()
  }
})


